// 效果系统 - 管理粒子效果、动画和视觉效果
class EffectSystem {
    static createParticles(scene) {
        // 创建粒子系统
        scene.particles = scene.add.particles(0, 0, 'spark', {
            scale: { start: 0.3, end: 0 },
            speed: { min: 30, max: 60 },
            lifespan: 200,
            quantity: 0
        });

        scene.bloodParticles = scene.add.particles(0, 0, 'blood', {
            scale: { start: 0.4, end: 0 },
            speed: { min: 20, max: 40 },
            lifespan: 300,
            quantity: 0,
            alpha: { start: 0.6, end: 0 }
        });

        scene.trailParticles = scene.add.particles(0, 0, 'trail', {
            scale: { start: 0.2, end: 0 },
            speed: { min: 5, max: 15 },
            lifespan: 150,
            quantity: 0,
            alpha: { start: 0.4, end: 0 }
        });
    }

    static createDamageText(scene, x, y, damage, isCrit) {
        const color = isCrit ? '#ffff00' : '#ffffff';
        const fontSize = isCrit ? '20px' : '16px';

        // 将坐标转换为屏幕坐标
        const screenX = x - scene.cameras.main.scrollX;
        const screenY = y - scene.cameras.main.scrollY;

        const damageText = scene.add.text(screenX, screenY, damage.toString(), {
            fontSize: fontSize,
            fill: color,
            fontWeight: 'bold',
            stroke: '#000000',
            strokeThickness: 1
        }).setOrigin(0.5).setScrollFactor(0).setDepth(2000);

        // 使用更平滑的动画，增加动画时间
        scene.tweens.add({
            targets: damageText,
            y: screenY - 40,
            alpha: { from: 1, to: 0 },
            duration: 1500,
            ease: 'Cubic.easeOut',
            onComplete: () => {
                damageText.destroy();
            }
        });
    }

    static createImpact(scene, x, y) {
        // 创建撞击效果
        if (scene.particles) {
            scene.particles.emitParticleAt(x, y, 3);
        }
        
        // 创建撞击火花
        const spark = scene.add.circle(x, y, 4, 0xffffff, 0.6);
        
        // 火花动画
        scene.tweens.add({
            targets: spark,
            scaleX: 1.5,
            scaleY: 1.5,
            alpha: 0,
            duration: 150,
            ease: 'Power2',
            onComplete: () => {
                spark.destroy();
            }
        });
    }

    static createExplosion(scene, x, y) {
        // 创建爆炸粒子
        const particles = scene.add.particles(x, y, 'particle', {
            speed: { min: 30, max: 80 },
            angle: { min: 0, max: 360 },
            scale: { start: 0.3, end: 0 },
            lifespan: 300,
            gravityY: 0,
            quantity: 1,
            frequency: 40,
            maxParticles: 6,
            alpha: { start: 0.6, end: 0 }
        });

        // 自动销毁粒子系统
        scene.time.delayedCall(300, () => {
            particles.destroy();
        });
    }

    static showFloatingText(scene, x, y, text, color = '#ffffff') {
        // 将坐标转换为屏幕坐标
        const screenX = x - scene.cameras.main.scrollX;
        const screenY = y - scene.cameras.main.scrollY;

        const floatingText = scene.add.text(screenX, screenY, text, {
            fontSize: '20px',
            fill: color,
            stroke: '#000000',
            strokeThickness: 2
        }).setOrigin(0.5).setScrollFactor(0).setDepth(2000);
        
        // 使用更平滑的动画，增加动画时间和距离
        scene.tweens.add({
            targets: floatingText,
            y: screenY - 50,
            alpha: { from: 1, to: 0 },
            duration: 2000,
            ease: 'Cubic.easeOut',
            onComplete: () => {
                floatingText.destroy();
            }
        });
    }

    static showAchievementUnlocked(scene, title, description) {
        const camera = scene.cameras.main;
        const x = camera.width / 2;
        const y = 100;
        
        // 创建成就解锁提示 - 固定在屏幕上
        const achievementBg = scene.add.rectangle(x, y, 400, 80, 0x000000, 0.8)
            .setStrokeStyle(2, 0xffd700)
            .setScrollFactor(0);
            
        const achievementTitle = scene.add.text(x, y - 20, '🏆 成就解锁: ' + title, {
            fontSize: '20px',
            fontFamily: 'Arial',
            fill: '#ffd700'
        }).setOrigin(0.5).setScrollFactor(0);
        
        const achievementDesc = scene.add.text(x, y + 10, description, {
            fontSize: '16px',
            fontFamily: 'Arial',
            fill: '#ffffff'
        }).setOrigin(0.5).setScrollFactor(0);
        
        // 使用更平滑的动画
        scene.tweens.add({
            targets: [achievementBg, achievementTitle, achievementDesc],
            alpha: { from: 1, to: 0 },
            delay: 3000,
            duration: 1500,
            ease: 'Cubic.easeOut',
            onComplete: () => {
                achievementBg.destroy();
                achievementTitle.destroy();
                achievementDesc.destroy();
            }
        });
    }
}